package com.ydsungan.service.impl;

import com.ydsungan.common.DateUtils;
import com.ydsungan.dao.CommentsMapper;
import com.ydsungan.dao.PostMapper;
import com.ydsungan.pojo.Comments;
import com.ydsungan.pojo.CommentsExample;
import com.ydsungan.pojo.Post;
import com.ydsungan.pojo.PostExample;
import com.ydsungan.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class CommunityServiceImpl implements CommunityService {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private CommentsMapper commentsMapper;

    @Override
    public Post selectByPostId(int id) {
        return postMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Post> selectByExample(PostExample postExample) {
        return postMapper.selectByExample(postExample);
    }

    @Override
    public List<Comments> selectCommentsByExample(CommentsExample commentsExample) {
        return commentsMapper.selectByExample(commentsExample);
    }

    @Override
    public int insertComments(Comments comments) {
        return commentsMapper.insert(comments);
    }

    @Override
    public int updatePostSelective(Post post) {
        return postMapper.updateByPrimaryKeySelective(post);
    }

    @Override
    public int updateCommentsSelective(Comments comments) {
        return commentsMapper.updateByPrimaryKeySelective(comments);
    }

    @Override
    public long countPosts(Boolean today) throws ParseException {
        PostExample postExample = new PostExample();
        if(today){
            PostExample.Criteria criteria = postExample.createCriteria();
            List<Date> dateList = DateUtils.getDateStartAndEnd(0);
            criteria.andCreateTimeBetween(dateList.get(0),dateList.get(1));
        }

        return postMapper.countByExample(postExample);
    }
}
